package Ms

import (
	"errors"
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/parDef"
	"db2s/ref"
	"reflect"
	"strings"
)

func (rows RowsF) Prefix(parameter parDef.Parameter) (result global.Return, err error) {
	var (
		tableName, ignoreStr string
		insertColumn         []string
	)
	if result, err = rows.InsertAllCol(parameter); err != nil {
		err = ref.ErrAddPrintf("SqlServer.Prefix", err)
		return
	}
	switch result.Result.(type) {
	case []string:
		insertColumn = result.Result.([]string)
	default:
		err = ref.ErrAddPrintf("SqlServer.Prefix", errors.New(fmt.Sprintf("type not match. curry type is %v", reflect.TypeOf(result.Result))))
	}
	tableName = fmt.Sprintf(" [%v].[%v] ", parameter.Object.Schema, parameter.Object.Table)
	result.Result = fmt.Sprintf("%s ", fmt.Sprintf("INSERT %v INTO %v ([%s]) VALUES", ignoreStr, tableName, strings.Join(insertColumn, "],[")))
	if parameter.Object.LobLogo {
		var s1 []string
		for i := 0; i < len(insertColumn); i++ {
			s1 = append(s1, "?")
		}
		result.Result = fmt.Sprintf("INSERT INTO %v ([%s]) VALUES (%s)", tableName, strings.Join(insertColumn, "],["), strings.Join(s1, ","))
	}
	return
}

func (rows RowsF) ExecDML(parameter parDef.Parameter) (result global.Return, err error) {
	return
}
func (rows RowsF) SelectIdxCol(parameter parDef.Parameter) (result global.Return, err error) {
	return
}
func (rows RowsF) SelectAllCol(parameter parDef.Parameter) (result global.Return, err error) {
	return
}
func (rows RowsF) InsertAllCol(parameter parDef.Parameter) (result global.Return, err error) {
	return
}
func (rows RowsF) StringValueCompare(parameter parDef.Parameter) (result global.Return, err error) {
	return
}
func (rows RowsF) IndexPart(parameter parDef.Parameter) (result global.Return, err error) {
	return
}
func (rows RowsF) SelectRowsSql(parameter parDef.Parameter) (result global.Return, err error) {
	return
}
func (rows RowsF) ReadData(parameter parDef.Parameter) (result global.Return, err error) {
	var event = "[ReadData]"
	var execSql []string
	defer func() {
		if r := recover(); r != nil {
			err = errors.New(fmt.Sprintf("%v defer recover An exception was captured. abnormal is %v ", event, r))
		}
	}()
	if parameter.Meta == nil {
		return
	}
	switch parameter.Meta.(type) {
	case string:
		execSql = append(execSql, parameter.Meta.(string))
	case []string:
		execSql = parameter.Meta.([]string)
	default:
		err = ref.ErrAddPrintf("SqlServer.ReadData", errors.New(fmt.Sprintf("type not matched. curry type %v", reflect.TypeOf(parameter.Meta))))
		return
	}
	if result, err = forExecQuerySql(event, parameter, ETL.GarthStringPointGarth, execSql); err != nil {
		err = ref.ErrAddPrintf("SqlServer.ReadData", err)
		return
	}
	return
}
func (rows RowsF) ReadMiss(parameter parDef.Parameter) (result global.Return, err error) {
	return
}
func (rows RowsF) InsertSql(parameter parDef.Parameter) (result global.Return, err error) {
	return
}
func (rows RowsF) Insert(parameter parDef.Parameter) (result global.Return, err error) {
	return
}
